package com.maxjay.component.simplelist.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maxjay.component.metadata.web.controller.BaseServlet;
import com.maxjay.component.simplelist.model.QueryConditionField;
import com.maxjay.component.simplelist.model.QueryScheme;
import com.maxjay.component.simplelist.service.QuerySchemeService;
import com.maxjay.component.simplelist.web.page.Page;
import com.maxjay.system.core.Application;

public class SimpleListServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
    public SimpleListServlet() {
        super();
    }

	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String schemeId = request.getParameter("schemeId");
		QueryScheme queryScheme = Application.getEntityContext().getQuerySchemeFatory().getQueryScheme(schemeId);
		request.setAttribute("queryScheme", queryScheme);
		QuerySchemeService querySchemeService = Application.getBeanContext().getBean("querySchemeService", QuerySchemeService.class);
//		String rowCountStr = request.getParameter("rowCount");
//		String pageIndexStr = request.getParameter("pageIndex");
		Map<String, Object> paramMap = parseQueryParamMap(queryScheme, request.getParameterMap());
		int rowCount = 0;
		int pageIndex = 1;
//		if(StringUtils.isNotBlank(rowCountStr)) {
//			rowCount = Integer.valueOf(rowCountStr);
//		} else {
			rowCount = querySchemeService.getRecordCount(queryScheme, paramMap);
//		}
//		if(StringUtils.isNotBlank(pageIndexStr)) {
//			pageIndex = Integer.valueOf(pageIndexStr);
//		}
		Page page = new Page(pageIndex, rowCount);
		request.setAttribute("page", page);
		request.setAttribute("paramMap", paramMap);
//		List<Record> displayRecords = querySchemeService.getDisplayRecordList(schemeId, paramMap, page.getStartRowIndex(), page.getEndRowIndex());
//		request.setAttribute("displayRecords", displayRecords);
		request.getRequestDispatcher(queryScheme.getQuerySchemeConfig().getViewConfig().getListPageLocation()).forward(request, response);
	}
    
    private Map<String, Object> parseQueryParamMap(QueryScheme queryScheme, Map<String, Object> paramArrMap) {
    	Map<String, Object> paramMap = new HashMap<String, Object>();
    	for (QueryConditionField conditionField : queryScheme.getConditionFields()) {
    		String paramName = conditionField.getEntityField().getName().toUpperCase();
    		String[] paramValArr = (String[])paramArrMap.get(paramName);
    		if (paramValArr == null) {
				continue;
			}
    		String originalVal = paramValArr[0];
//    		String convertVal = EncodeUtil.convertStrEncode(originalVal, EncodeUtil.ISO_8859_1, EncodeUtil.UTF_8);
    		paramMap.put(paramName, originalVal);
		}
		return paramMap;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
